﻿using System;
using System.Web.Mvc;
using log4net;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.BLL.WxPayAPI;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Enum;
using System.Linq;

namespace Matrix.MaKaYunShang.McMagWeb.Controllers
{
    public class PayResultController : Controller
    {
        public readonly ILog Logger = LogManager.GetLogger(typeof(BaseController));

        /// <summary>
        /// 支付结果通知url
        /// </summary>
        public ContentResult ResultNotifyPage()
        {
            Logger.Info("PayResult|ResultNotifyPage| Method In");
            try
            {
                var now = DateTime.Now;
                var resultNotify = new ResultNotify(System.Web.HttpContext.Current);
                var notifyData = resultNotify.GetNotifyData();
                var return_code = notifyData.GetValue("return_code").ToString();
                var transaction_id = notifyData.GetValue("transaction_id").ToString();
                var out_trade_no = notifyData.GetValue("out_trade_no").ToString();
                Logger.Info("PayResult|ResultNotifyPage| return_code:" + return_code + "  transaction_id:" + transaction_id + "  out_trade_no:" + out_trade_no);
                var shopMoneyChangeRecord = ShopMoneyChangeRecordBLL.Instance.GetModel(int.Parse(out_trade_no));
                if (shopMoneyChangeRecord != null)
                {
                    if (return_code == "SUCCESS")
                    {
                        shopMoneyChangeRecord.OperaResult = (int)CommonEnum.OperaResult.操作成功;
                        shopMoneyChangeRecord.ChangeSerialNumber = transaction_id;
                        ShopMoneyChangeRecordBLL.Instance.Update(shopMoneyChangeRecord);
                        var shopSubsidiaryInfo = ShopSubsidiaryInfoBLL.Instance.GetModelByShopId(shopMoneyChangeRecord.ShopId);
                        if (shopSubsidiaryInfo != null)
                        {
                            var sysConfig = SysConfigBLL.Instance.GetModelList().FirstOrDefault();
                            if (sysConfig != null)
                            {
                                shopSubsidiaryInfo.AccountMoney += shopMoneyChangeRecord.ChangeMoney;
                                shopSubsidiaryInfo.TotaChargeMoney += shopMoneyChangeRecord.ChangeMoney;
                                shopSubsidiaryInfo.TotalCanSendCard += int.Parse(Math.Floor(shopMoneyChangeRecord.ChangeMoney).ToString()) * sysConfig.CardQuantityPerYuan;
                                shopSubsidiaryInfo.UpdateTime = now;
                                ShopSubsidiaryInfoBLL.Instance.Update(shopSubsidiaryInfo);
                            }
                        }
                    }
                    else
                    {
                        shopMoneyChangeRecord.OperaResult = (int)CommonEnum.OperaResult.操作失败;
                        shopMoneyChangeRecord.ChangeSerialNumber = transaction_id;
                        ShopMoneyChangeRecordBLL.Instance.Update(shopMoneyChangeRecord);
                    }
                }
                Response.ContentType = "text/xml";
                var checkResult = resultNotify.ProcessNotifyForReCall(notifyData);
                Logger.Info(checkResult);
                Response.Write(checkResult);
                Response.End();
            }
            catch (Exception ex)
            {
                Logger.Error("PayResult|ResultNotifyPage| Ex:" + ex);
            }
            return Content(null);
        }
    }
}
